package com.franklin.ideaplugin.easytesting.ui.execute;

import cn.hutool.core.collection.CollectionUtil;
import com.franklin.ideaplugin.api.collection.map.RecursionMap;
import com.franklin.ideaplugin.api.ui.IBaseComponent;
import com.franklin.ideaplugin.api.utils.ProjectUtils;
import com.franklin.ideaplugin.api.utils.PsiUtil;
import com.franklin.ideaplugin.api.utils.UiUtils;
import com.franklin.ideaplugin.easytesting.rpc.entity.MethodParameter;
import com.franklin.ideaplugin.easytesting.ui.Sizes;
import com.google.common.collect.Maps;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.ui.components.JBLabel;
import lombok.AllArgsConstructor;

import javax.swing.*;
import java.awt.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Ye Junhui
 * @since 2023/8/29
 */
@AllArgsConstructor
public class BaseExecuteForm implements IBaseComponent {

    private final PsiMethod psiMethod;
    private final JPanel methodParamPanel;
    private final List<PsiParameter> psiParameterList;
    private List<String> instanceMethodEnumList = Collections.emptyList();
    private final Map<String, Object> historyParamMap;
    /**
     * 缓存填充值
     */
    private final Map<String, ParameterTextComponent> paramMap;
    private final Map<String, PsiParameter> psiParamMap;
    private final Map<String, MethodParameter> executeParamMap;


    /**
     * 方法参数
     */
    public void initMethodParamPanel() {
        //方法参数
        //参数表单
        IParameterFormProvider parameterFormProvider = IParameterFormProvider.getInstance(ProjectUtils.getCurrProject());
        Dimension dimension = new Dimension(-1, (int) (this.psiParameterList.size() * Sizes.ExecuteMethodFormSize.CHILD_SIZE.getHeight()) + 10);
        this.methodParamPanel.setSize(dimension);
        this.methodParamPanel.setPreferredSize(dimension);
        for (int i = 0; i < this.psiParameterList.size(); i++) {
            int k = CollectionUtil.isEmpty(this.instanceMethodEnumList) ? i : i + 1;
            PsiParameter psiParameter = psiParameterList.get(i);
            //参数名
            String label = "   "
                    + psiParameter.getType().getPresentableText()
                    + "   "
                    + psiParameter.getName();
            JBLabel jbLabel = new JBLabel(
                    UiUtils.splitLine(label, Sizes.ExecuteMethodFormSize.PARAM_NAME_SIZE.width)
            );
            jbLabel.setBorder(BorderFactory.createLoweredBevelBorder());
            this.addCell(methodParamPanel, jbLabel, k, 0, Sizes.ExecuteMethodFormSize.PARAM_NAME_SIZE);

            //参数输入框
            //添加默认值
            Object defaultValueOfPsiType = PsiUtil.getDefaultValueOfPsiType(psiParameter.getType(), new RecursionMap<>());
            Object defaultValue = historyParamMap.getOrDefault(psiParameter.getName(), defaultValueOfPsiType);
            ParameterTextComponent parameterTextComponent = parameterFormProvider.getForm(psiParameter, defaultValueOfPsiType, defaultValue);
            this.paramMap.put(psiParameter.getName(), parameterTextComponent);
            this.psiParamMap.put(psiParameter.getName(), psiParameter);

            JComponent textField = parameterTextComponent.getComponent();
            this.addCell(methodParamPanel, textField, k, 1, Sizes.ExecuteMethodFormSize.CHILD_SIZE);

        }
    }


    @Override
    public JComponent getMainPanel() {
        return null;
    }
}
